const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Prepayment_Scheme_Name: [{ required: true, message: '请输入储值方案名称', trigger: 'blur' }],
        Starting_Amount: [{ required: true, message: '请输入起步值', trigger: 'blur' }],
        Unit_Amount: [{ required: true, message: '请输入单位值', trigger: 'blur' }],
        Gift_Amount: [{ required: true, message: '请输入赠送值', trigger: 'blur' }],
        Execute_Date: [{ required: true, message: '请选择执行时间', trigger: 'change' }],
      },
      type: '',
      form: {     //储值方案
        Prepayment_Scheme_SerialNumber: '',
        Prepayment_Scheme_Name: '',
        Starting_Amount: '',
        Unit_Amount: '',
        Gift_Amount: '',
        Execute_Date: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          prepayment_scheme_storescope: [{     //储值方案适用店铺
            Prepayment_Scheme_StoreScope_SerialNumber: "",
            Prepayment_Scheme_SerialNumber: '',
            Store_SerialNumber: '',
            App_ID: ''
          }]
        }
      },
      store: [],
      loading: false
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      let _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: 'store_info',
          token: $.cookie("token")
        },
        success: function (res) {
          _this.store = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      _this.form.Starting_Amount = Math.floor(_this.form.Starting_Amount)
      _this.form.Unit_Amount = Math.floor(_this.form.Unit_Amount)
      _this.form.Gift_Amount = Math.floor(_this.form.Gift_Amount)
      _this.form.Children.prepayment_scheme_storescope.forEach(item => {
        if (item.App_ID != '') {
          console.log(_this.form)
          this.$refs[formName].validate((valid) => {
            if (valid) {
              _this.loading = true
              $.ajax({
                url: DomainName + '/getAddAction',
                type: 'POST',
                data: {
                  Data: JSON.stringify({
                    prepayment_scheme: [_this.form]
                  }),
                  token: $.cookie("token")
                },
                success: function (res) {
                  if (res.Status === 'success') {
                    _this.$message.success("提交成功")
                    window.location.href = './StoredValue.html'
                  } else {
                    _this.$message.error("提交失败，请稍后再试")
                  }
                },
                error: function () {
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                  _this.loading = false
                  if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                    window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  }
                }
              })
            } else {
              console.log("error submit!!");
              return false;
            }

          });

        } else {
          _this.$message.error("没有对应的小程序")
        }
      })

    },
    Change_Store_SerialNumber(index) {  //选择店铺获取对应App_ID
      let _this = this
      $.ajax({
        url: DomainName + '/getprepaymentSchemeAppIDAction',
        type: 'POST',
        data: {
          Store_SerialNumber: _this.form.Children.prepayment_scheme_storescope[index].Store_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          if (res.length == 1) {
            _this.form.Children.prepayment_scheme_storescope[index].App_ID = res[0].App_ID
          } else {
            _this.$message.error("该店铺未绑定App_ID")
            _this.form.Children.prepayment_scheme_storescope[index].App_ID = ''
          }
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    add_prepayment_scheme_storescope() {
      this.form.Children.prepayment_scheme_storescope.push({     //储值方案适用店铺
        Prepayment_Scheme_StoreScope_SerialNumber: "",
        Prepayment_Scheme_SerialNumber: '',
        Store_SerialNumber: '',
        App_ID: ''
      })
    },
    remove_prepayment_scheme_storescope() {
      if (this.form.Children.prepayment_scheme_storescope.length > 1) {
        this.form.Children.prepayment_scheme_storescope.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
  }
})